<!-- subpkg_medicine/payment/index.vue -->
<script lang="ts" setup>
  // 定义响应式数据
  // 封装函数 调用请求函数
  // 调用函数

  import { preOrderApi } from '@/apis/order'
  import { addressListApi, createOrderApi2 } from '@/apis/order'
  import type { Address, OrderPre } from '@/types/order'
  import { request } from '@/utils/http'
  import { ref } from 'vue'

  // 1. 收货地址信息
  const addressInfo = ref({} as Address)
  // 用户收货地址
  const getAddressList = async () => {
    // 地址列表接口
    const data = await addressListApi()
    // 渲染地址列表数据
    addressInfo.value = data[0] // 第0项=>默认地址
  }
  // 获取收货地址列表
  getAddressList()

  // 获取页面路径中的处方id
  const props = defineProps<{ id: string }>()
  console.log(props)

  // 预付订单信息
  const preOrderInfo = ref({} as OrderPre)
  // 药品预付订单
  const getPreOrder = async () => {
    if (!props.id) return
    // 预付订单接口
    const data = await preOrderApi(props.id)
    // 渲染预付订单信息
    preOrderInfo.value = data
  }
  getPreOrder()

  // 定义组件标记
  const payRef = ref()

  const clickPay = async () => {
    // 创建订单(接口)
    if (orderId.value === '') {
      // 生成待支付订单接口
      const data = await createOrderApi2(props.id, addressInfo.value.id) // 处方id   地址id
      // 传递订单ID
      orderId.value = data.id
    }

    // 打开UI选择支付方式之前
    payRef.value.open()
  }

  // 3. 待支付订单ID
  const orderId = ref('') // 一会 会创建订单得到id

  const pay = async (index: number) => {
    // 请求接口获取支付链接
    const res = await request.post<null, { payUrl: string }>('/patient/consult/pay', {
      paymentMethod: index, // 支付方式
      orderId: orderId.value, // 订单id
      payCallback: 'http://localhost:5173/#/subpkg_medicine/pay_result/index', // 支付完成的回跳地址
    })

    // 跳转支付链接
    location.href = res.payUrl
  }
</script>

<template>
  <scroll-view background-color="#f6f6f6">
    <view class="medicine-page">
      <view class="page-header">
        <view class="order-shippment">
          <view class="region">
            <uni-icons size="16" color="#FF7702" type="location-filled" />
            {{ addressInfo.province }} {{ addressInfo.city }}
          </view>
          <view class="detail">{{ addressInfo.addressDetail }}</view>
          <view class="receiver">{{ addressInfo.receiver }} {{ addressInfo.mobile }}</view>
        </view>
      </view>

      <view class="order-shop">
        优医药房
        <text class="alt">优医质保 假一赔十</text>
      </view>
      <!-- 药品列表 -->
      <view class="medicine-list">
        <view class="medicine-list-item" v-for="item in preOrderInfo.medicines" :key="item.id">
          <image class="medicine-cover" :src="item.avatar" mode="aspectFill" />
          <view class="medicine-info">
            <text class="name">{{ item.name }}</text>
            <text class="unit symbol">24片</text>
            <text class="price">¥{{ item.amount }}</text>
          </view>
          <view class="quantity">x{{ item.quantity }}</view>
          <view class="guide">用法用量：{{ item.usageDosag }}</view>
        </view>
      </view>

      <!-- 订单信息 -->
      <view class="order-info">
        <uni-list :border="false">
          <uni-list-item title="药品金额" :right-text="`¥${preOrderInfo.payment}`" />
          <uni-list-item title="运费" :right-text="`¥${preOrderInfo.expressFee}`" />
          <uni-list-item
            title="优惠券"
            show-arrow
            :right-text="`-¥${preOrderInfo.couponDeduction}`"
          />
          <uni-list-item title="实付款" :right-text="`¥${preOrderInfo.actualPayment}`" />
        </uni-list>
      </view>

      <!-- 底部 -->
      <view class="toolbar">
        <view class="total-amount">
          需付款: <text class="number">¥{{ preOrderInfo.actualPayment }}</text>
        </view>
        <view class="buttons">
          <button class="uni-button" @click="clickPay">立即支付</button>
        </view>
      </view>
    </view>

    <custom-payment
      ref="payRef"
      :amount="preOrderInfo.actualPayment"
      :orderId="orderId"
      @comfirm="pay"
    ></custom-payment>
  </scroll-view>
</template>

<style lang="scss">
  @import './index.scss';
</style>
